<?php

namespace Project\AuthentificationBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Project\AuthentificationBundle\Session\SessionManager;

class AuthentificationController extends Controller {

    public function loginAction() {
        return $this->render('ProjectAuthentificationBundle:Authentification:login.html.twig', array('error' => ""));
    }

    public function deconnexionAction() {
        $sessionManager = new SessionManager($this->get('session'));
        $sessionManager->deconnexion();
        return $this->redirect($this->generateUrl('project_authentification_login'));
    }
    
        public function loginCheckAction() {

        $request = $this->getRequest();
        $sessionManager = new SessionManager($this->get('session'), $request);
        $mailentered = $sessionManager->getMailuser();
        $passentered = $sessionManager->getPassuser();
        $manager = $this->getDoctrine()->getManager();

        $personnelistdb = $manager
                ->getRepository('ProjectScrumBundle:Utilisateur')
                ->findBy(
                array('email' => $mailentered,
                    'password' => $passentered));

        //Si personnelist n'est pas vide, l'utilisateur à entrer des info correct
        if (!empty($personnelistdb)) {
            $sessionManager->setSession($personnelistdb[0]);
            return $this->redirect($this->generateUrl('project_scrum_profil'));
        } else {
            $error = "Login ou mot de pass incorrect";
            return $this->render('ProjectAuthentificationBundle:Authentification:login.html.twig', array('error' => $error));
        }
    }

}
